<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The User class for table 'user'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.33
 * 
 * 0.3)  Adds the 'getExpcilitUser' function.
 * 0.31) Adds the 'updateLasttime' function.
 * 0.32) Adds the 'getTitleByUserid' function.
 * 0.33) Changes the 'lasted' column to 'loggedincnt'.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
require_once INSTALLDIR . '/lib/slh_util.php';

// Type enumeration.
define('USER_TYPE_PROFILE',		1);
define('USER_TYPE_ASSOCIATION',	2);

define('USER_STATUS_VERIFIED',	1);			// use it normally
define('USER_STATUS_VERIFYING',	2);			// useless before verified
define('USER_STATUS_BLOCK',		3);			// blocks and deprecated currently

class User extends Memcached_DataObject
{
	public $__table = 'user';				// table name
	public $id;								// int(11)  primary_key not_null
	public $status;							// tinyint(4)
	public $username;						// varchar(255)  not_null
	public $password;						// varchar(255)  not_null
	public $type;							// tinyint(4)
	public $loggedincnt;					// int(11)
	public $modified;						// timestamp()  not_null
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('User', $key, $value); 
	}
	
	/**
	 * Get the expcilit user.
	 */
	function getExpcilitUser()
	{
		if(USER_TYPE_PROFILE == $this->getUserType()) {
			$profile = Memcached_DataObject::staticGet('Profile', 'user_id', $this->id);
			return $profile; 
		} else {
			$association = Memcached_DataObject::staticGet('Association', 'user_id', $this->id);
			return $association; 
		}
	}
	
	/**
	 * Get the user type, profile user or association user?
	 */
	function getUserType()
	{
		return $this->type;
	}
	
	/**
	 * Get the user's title(real name or association name).
	 */
	function getUserTitle()
	{
		$query = 'SELECT `type` FROM `user` WHERE id = ' . $this->id;
		
		$user = new User();
		$user->query($query);
		if ($user->fetch()) {
			if(USER_TYPE_PROFILE == $user->type) {
				$profile = Memcached_DataObject::staticGet('Profile', 'user_id', $this->id);
				return $profile->realname; 
			} else {
				$association = Memcached_DataObject::staticGet('Association', 'user_id', $this->id);
				return $association->cnname; 
			}
		}
		
		return false;
	}
	
	/**
	 * Update the count of the logged in time in the user column.
	 */
	function updateLoggedin()
	{
		$now = slh_sql_now();
		$query = 'UPDATE `user` SET `loggedincnt` = `loggedincnt` + 1' .
				 ' WHERE `id` = ' . $this->id;
		
		return $this->query($query);
	}
	
	/**
	 * Get user title by user_id.
	 */
	static function getTitleByUserid($user_id)
	{
		$pquery = 'SELECT `realname` FROM `profile` WHERE `user_id` = ' . $user_id;
		$profile = new Profile();
		$profile->query($pquery);
		if ($profile->fetch()) {
			$realname = $profile->realname;
			$profile->free();
			return $realname;
		}
		
		// Second the association.
		$aquery = 'SELECT `cnname` FROM `association` WHERE `user_id` = ' . $user_id;
		$association = new Association();
		$association->query($aquery);
		if ($association->fetch()) {
			$cnname = $association->cnname;
			$association->free();
			return $cnname;
		}	
		
		return 'N/A';
	}
	
	
	/**
	 * Check if the 'user'.'id' exists.
	 */
	static function hasUserId($id)
	{
		$query = 'SELECT `id` FROM `user` WHERE `id` = ' . $id;

		$user = new User();
		$user->query($query);
		
		if ($user->fetch()) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Reset the password of the uesr.
	 */
	static function resetPassword($username, $newpassword)
	{
		if (empty($username)) {
			return false;
		}
		
		$user = User::staticGet('username', $username);
		if ( !empty($user)) {
			if ( !empty($newpassword)) {
				$origUser = clone($user); 
				$user->password = $newpassword;
				
				$result = $user->update($origUser);
				if ( !empty($result)) {
					return true;
				} else {
					return false;
				}
			}
		}
	}
	
	/**
	 * Authenticate if a username exists and has matching password.
	 */
	static function authenticateUser($username, $password)
	{
		if (empty($username)) {
			return false;
		}
		
		$authenticatedUser = false;
		
		$user = User::staticGet('username', $username);
		if ( !empty($user)) {
			if ( !empty($password)) {
				if (0 == strcmp($password, $user->password)) {
					$authenticatedUser = $user;
				}
			}
		}
		
		return $authenticatedUser;
	}
	
	/**
	 * Get the user's type by given id.
	 */
	static function getUserTypeById($id)
	{
		$query = 'SELECT `type` FROM `user` WHERE id = ' . $id;
		
		$user = new User();
		$user->query($query);
		if ($user->fetch()) {
			return $user->type;
		}
		
		return false;
	}
}
?>